package com.weiz.thread.threadpool;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

@Configuration
public class ThreadPoolConfig {
    @Bean("threadPoolExecutorNormal")
    public ThreadPoolExecutor threadPoolExecutor() {
        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
               .setNameFormat("threadPoolExecutorNormal-%d").build();
        ThreadPoolExecutor pool = new ThreadPoolExecutor(5, 200,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory,
                new ThreadPoolExecutor.AbortPolicy());
        // 模拟让线程池运行 1 小时
        new Thread(() -> {
            try {
                TimeUnit.HOURS.sleep(1);
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            } finally {
                pool.shutdown();
            }
        }).start();
        return pool;
    }
}